/**
 * Created by erichua on 22/12/2017.
 */
import {APIClient} from '@webank/trident'
import CGI from './cgi'

export default {
  component: require('./AppContainer').default,
  initialState: {
    balance: undefined
  },
  actions: {
    transferOut: out => out,
    updateBalance: amount => amount
  },
  asyncActions: (actions) => ({
    queryBalance: () => async (dispatch) => {
      APIClient.request(CGI.queryBalance)
        .then(({data}) => {
          dispatch(actions.updateBalance(data.balance))
        })
    },
  }),
  reducers: {
    transferOut: (state, action) => ({
      ...state,
      balance: state.balance - action.payload
    }),
    updateBalance: (state, action) => ({
      ...state,
      balance: action.payload
    })
  },
}
